Skip to content

fix: prevent division by zero when pageSize is 0 or negative#8271

Closed
appfast-codev wants to merge 1 commit into
wenzhixin:developfrom
appfast-codev:fix/pageSize-div-zero
Closed

fix: prevent division by zero when pageSize is 0 or negative#8271
appfast-codev wants to merge 1 commit into
wenzhixin:developfrom
appfast-codev:fix/pageSize-div-zero

Conversation

@appfast-codev
Copy link
Copy Markdown

In initPagination, if pageSize is 0 or negative, the calculation ~~((totalRows - 1) / pageSize) + 1 causes a division by zero. Added a guard to treat pageSize <= 0 the same as 'show all rows', which sets pageSize to totalRows and allSelected to true. All 339 existing tests pass.

@wenzhixin
Copy link
Copy Markdown
Owner

Please provide an Online Example to show your problem. Thanks!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Guards pagination calculations against invalid pageSize values to prevent division-by-zero during totalPages computation in the core pagination module.

Changes:

  • Treat pageSize <= 0 as “show all rows” during pagination initialization.
  • Set pageSize to totalRows and mark the “all rows” option as selected for rendering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/modules/pagination.js
Comment on lines +53 to +56
if (opts.pageSize <= 0) {
opts.pageSize = opts.totalRows
allSelected = true
}
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pageSize <= 0 normalization is inside if (opts.totalRows), but pageFrom/pageTo are calculated later unconditionally using opts.pageSize. If totalRows is 0 (or otherwise falsy) and pageSize is 0/negative, this leaves an invalid page range (e.g. pageFrom becomes 1 while pageTo can be 0/negative), which can leak into pagination info rendering (notably when onlyInfoPagination is enabled, the pagination container isn't auto-hidden for empty data). Consider normalizing opts.pageSize (or using a local effective page size) before the opts.totalRows check, and ensure arithmetic uses a positive page size even when totalRows is 0.

Copilot uses AI. Check for mistakes.
Comment thread src/modules/pagination.js
Comment on lines +53 to +56
if (opts.pageSize <= 0) {
opts.pageSize = opts.totalRows
allSelected = true
}
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes a crash/NaN behavior for pageSize <= 0, but there doesn't appear to be a regression test covering pageSize set to 0 or a negative value. Adding a Cypress test case that initializes a table with pagination: true and pageSize: 0 (and -1) and asserts pagination renders without errors and behaves like “all rows” would help prevent this from regressing.

Copilot uses AI. Check for mistakes.
@wenzhixin
Copy link
Copy Markdown
Owner

Thanks for the review! Here are some suggestions:

1. Move the guard before if (opts.totalRows)

The current guard only normalizes pageSize when totalRows is truthy, but pageFrom/pageTo are calculated unconditionally afterwards. When totalRows is 0 and pageSize is 0 or negative, this leads to an invalid page range (pageFrom=1, pageTo=0). Consider moving the guard before the if (opts.totalRows) check:

if (opts.pageSize <= 0) {
  opts.pageSize = opts.totalRows || 1
}

Note the || 1 fallback — when totalRows is 0, setting pageSize to 0 would still cause issues in pageFrom/pageTo calculations.

2. Add console.warn for invalid input

A pageSize of 0 or negative is likely a caller configuration error. Silently correcting it could mask the real problem. Consider adding a warning:

if (opts.pageSize <= 0) {
  console.warn('pageSize must be a positive number, falling back to show all rows.')
  opts.pageSize = opts.totalRows || 1
}

@wenzhixin
Copy link
Copy Markdown
Owner

Thanks for the PR! I've addressed the review feedback and created #8294 with the following improvements based on your work:

  1. Moved the pageSize <= 0 guard before the if (opts.totalRows) check to also cover the pageFrom/pageTo calculation.
  2. Added console.warn to help developers detect invalid pageSize configuration.

I'll close this PR in favor of #8294. Thanks again for the contribution!

@wenzhixin
Copy link
Copy Markdown
Owner

Closing in favor of #8294 which includes the fix with additional improvements.

@wenzhixin wenzhixin closed this Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants